<?php

namespace app\controller\admin;

use app\base\BaseEncryptController;
use app\model\company\CompanyBasicModel;
use think\Request;

/**
 * CompanyBasic
 * Class CompanyBasic
 * @package app\controller
 */
class CompanyBasic extends BaseEncryptController
{
    protected function initialize()
    {
        parent::initialize();
        $this->model = new CompanyBasicModel();
    }

    /**
     * 商品基本信息
     * @return \think\response\Json
     */
    public function getInfo()
    {
        $company = $this->model->where('id', '>', 0)->find();
        if (!empty($company)) {
            return self::returnMsg(1, $company);
        } else {
            return self::returnMsg(1, []);
        }
    }

    /**
     * 保存
     * @param Request $request
     * @return \think\response\Json
     */
    public function onSave(Request $request)
    {
        $formData = [
            'id' => $request->param('id'),
            'store_desc' => $request->param('store_desc'),
            'address' => $request->param('address'),
            'longitude' => $request->param('longitude'),
            'latitude' => $request->param('latitude'),
            'phone' => $request->param('phone'),
            'we_chat' => $request->param('we_chat'),
            'cell_phone' => $request->param('cell_phone'),
            'email' => $request->param('email'),
            'qr_code_1' => $request->param('qr_code_1'),
            'qr_code_2' => $request->param('qr_code_2')
        ];
        if (empty($formData['id'])) {
            $validate = 'app\validate\CompanyBasicValidate.add';
        } else {
            $validate = 'app\validate\CompanyBasicValidate';
        }
        $result = $this->validate($formData, $validate);
        if (true !== $result) {
            return self::returnMsg(991, [], $result);
        } else {
            $ret = $this->model->saveData($formData, true, true);
            if (false !== $ret['flag']) {
                return self::returnMsg(1);
            } else {
                return self::returnMsg(0);
            }
        }
    }
}
